package com.englisheveryday.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.englisheveryday.R;
import com.englisheveryday.constants.Constants;
import com.englisheveryday.service.SystemService;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-10-14 下午6:30:58
 * @version 1.1
 */

public class FeedBackActivity extends Activity{

	private Button feedBack_back,feedBack_save;
	private SystemService systemService;
	private EditText content;
	private Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback_activity);
		setViews();
		setListeners();
		handler = new FeedBackHandler();
		systemService = new SystemService(this);
	}
	
	private void setViews(){
		feedBack_back = (Button)findViewById(R.id.feedback_back);
		feedBack_save = (Button)findViewById(R.id.feedback_save);
		content = (EditText)findViewById(R.id.feedbacktext);
	}
	
	private void setListeners(){
		feedBack_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				FeedBackActivity.this.finish();
			}
		});
		feedBack_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String text = content.getText().toString();
				if(text==null||"".equals(text)){
					Toast.makeText(FeedBackActivity.this,R.string.feedback_not_null, Toast.LENGTH_SHORT).show();
					return;
				}
				Toast.makeText(FeedBackActivity.this,R.string.feedback_sending, Toast.LENGTH_SHORT).show();
				new Thread(new FeedBackThread(text)).start();
			}
		});
	}
	
	//创建Handler
	class FeedBackHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case Constants.SYSTEM_FEEDBACK_SUCCESS : {
				Toast.makeText(FeedBackActivity.this, R.string.feedback_send_success,Toast.LENGTH_SHORT).show();
				FeedBackActivity.this.finish();
			}
			break;
			case Constants.SYSTEM_FEEDBACK_FAILURE : {
				Toast.makeText(FeedBackActivity.this, R.string.feedback_send_failure,Toast.LENGTH_SHORT).show();
				return;
			}
		}
			
			
			super.handleMessage(msg);
		}
		
	}
	
	//创建反馈线程
	class FeedBackThread implements Runnable{
		private String text ;
		public FeedBackThread(String text){
			this.text = text;
		}
		
		@Override
		public void run() {
		  	if(systemService.feedback(text)){
		  		handler.sendEmptyMessage(Constants.SYSTEM_FEEDBACK_SUCCESS);
		  	}else {
		  		handler.sendEmptyMessage(Constants.SYSTEM_FEEDBACK_FAILURE);
		  	}
		}
	}
}

